/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */
 
package jdbcmanager.dbtools.datatypes;

import jdbcmanager.dbtools.*;
import jdbcmanager.dbtools.agentwise.*;

/**
 * Describes objects of a DataBase: catalogs, schemas, tables, etc.
 *  
 * @author Francisco Morero Peyrona
 */
public final class DbEntityDescriptor implements Cloneable
{
   private String sCatalog;
   private String sSchema;
   private String sName;
   private String sType;
   private Object oCargo; 

   //-------------------------------------------------------------------------//
   
   /**
    * Argument zero class constructor
    */
   public DbEntityDescriptor()
   {
      this( null, null, null );
   }
   
   /**
    * Class contructor
    * 
    * @param sCatalog
    * @param sSchema
    * @param sName
    */      
   public DbEntityDescriptor( String sCatalog, String sSchema, String sName )
   {
      this( sCatalog, sSchema, sName, null );
   }
   
   /**
    * 
    * @param sCatalog
    * @param sSchema
    * @param sName
    * @param sType
    */
   public DbEntityDescriptor( String sCatalog, String sSchema, String sName, String sType )
   {
      this.sCatalog = sCatalog;
      this.sSchema  = sSchema;
      this.sName    = sName;
      this.sType    = sType;
      this.oCargo   = null;
   }
   
   public String getCatalog()       { return this.sCatalog; }
   public String getSchema()        { return this.sSchema;  }
   public String getName()          { return this.sName;    }
   public String getType()          { return this.sType;    }
   public Object getCargo()         { return this.oCargo;   }
   public String getQualifiedName() { return _getQName();   }   // read-only
   
   public void setCatalog( String s )   { this.sCatalog    = s; }
   public void setSchema( String s )    { this.sSchema     = s; }
   public void setName( String s )      { this.sName       = s; }
   public void setType( String s )      { this.sType       = s; }
   public void setCargo( Object o )     { this.oCargo      = o; }
   
   public Object clone()
   {
      try
      {
         return (DbEntityDescriptor) super.clone();
      }
      catch( CloneNotSupportedException exc )
      {
         throw new InternalError();    // This should not happen
      }
   }
   
   /**
    * Note:<br> 
    * This method returns just the Name (instead of all the information stored in this class). <br>
    * It works in this way because the tree nodes invoke this method to show the node label.
    * 
    * @return 
    */
   public String toString()
   {
      return this.sName;
   }
   
   //-------------------------------------------------------------------------//
   
   private String _getQName()    // Get QualifiedName
   { 
      AgentMetaData amd = DbUtil.getAgentDBMS().getAgentMetaData();
      
      boolean bCatalogAtStart  = amd.isCatalogAtStart();  
      boolean bSupportsSchemas = amd.supportsSchemasInDataManipulation();
      String  sCatalogSep      = amd.getCatalogSeparator();
      String  sQuote           = amd.getIdentifierQuoteString();
      String  sCatalogName     = "";
      String  sSchemaName      = "";
      String  sEntityName      = "";
      String  sRet;
      
      if( sQuote == null )
         sQuote = "";
      
      if( sCatalogSep != null        &&
          sCatalogSep.length() > 0   &&
          this.sCatalog != null      &&
          this.sCatalog.length() > 0 )
      {
         sCatalogName = sQuote + this.sCatalog + sQuote;
      }

      if( bSupportsSchemas && this.sSchema != null && this.sSchema.length() > 0 )
      {
         sSchemaName = sQuote + this.sSchema + sQuote;
      }

      sEntityName = sQuote + this.sName + sQuote;
      
      if( sSchemaName.length() > 0 )
         sRet = sSchemaName +'.'+ sEntityName;
      else
         sRet = sEntityName; 
      
      if( sCatalogName.length() > 0 )
      {
         if( bCatalogAtStart ) 
            sRet = sCatalogName + sCatalogSep + sRet;
         else
           sRet = sRet + sCatalogSep + sCatalogName;
      }
        
      return sRet;
   }
}